/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transform;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.xmlbeans.XmlObject;

/**
 *
 * @author emiliano
 */
class TransformadorRequest {

    String transformar(XmlObject xmlObject) {
        TransformerFactory f = TransformerFactory.newInstance();
        StringWriter stringWriter = new StringWriter();
        
        Transformer t;
        try {
            InputStream templateInputStream = getClass().getResourceAsStream("/template.xsl");
            t = f.newTransformer(new StreamSource(templateInputStream));
            Source s = new StreamSource(xmlObject.newInputStream());
            Result r = new StreamResult(stringWriter);
            t.transform(s, r);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(TransformadorRequest.class.getName()).log(Level.SEVERE, null, ex);
        }catch (TransformerException te){
            Logger.getLogger(TransformadorRequest.class.getName()).log(Level.SEVERE, null, te);
        }

        return stringWriter.toString();
    }
}
